var layer = layui.layer
var form = layui.form

// 添加类别弹出层
var CateIndex = null;
$('#cate_btn').on('click', function () {
    CateIndex = layer.open({
        title: '添加文章分类',
        type: 1,
        area: ['500px', '300px'],
        btn: [],
        content: $('#layer-out').html()
    });
})

//添加新增类别功能实现
$('body').on('submit', '#addcate', function (e) {
    e.preventDefault()
    var addCatesInfo = $('#addcate').serialize()
    addBookCate(addCatesInfo)
    layer.close(CateIndex)
})


//封装函数向服务器新增文章分类请求
function addBookCate(bookinfo) {
    $.ajax({
        type: 'POST',
        url: '/my/article/addcates',
        data: bookinfo,
        success: function (res) {
            if (res.status !== 0) return layer.msg(res.message)
            layer.msg(res.message)
            getBookList()
        }
    })
}



getBookList()
//封装函数发送ajax请求向服务器索取图书列表
function getBookList() {
    $.ajax({
        type: 'GET',
        url: '/my/article/cates',
        success: function (res) {
            if (res.status !== 0) return layer.msg(res.message)
            var htmStr = template('bookList', res)
            $('.layui-card-body tbody').html(htmStr)

        }
    })
}

//弹出当前编辑按钮修改页面
$('.layui-card-body tbody').on('click', '#modify', function () {
    CateIndex = layer.open({
        title: '修改当前文章信息',
        type: 1,
        area: ['500px', '300px'],
        btn: [],
        content: $('#layer-modify').html()
    });
    var bookId = $(this).attr('data-id')
    $('#hid-id').attr('value', bookId)
    getThisBook(bookId)
})

//修改当前分类类别和别名
$('body').on('submit', '#modifyInfo', function (e) {
    e.preventDefault()
    var updataCate = $('#modifyInfo').serialize()
    modifyCate(updataCate)
    layer.close(CateIndex)

})


//弹出当前分类的删除询问窗口
$('.layui-card-body tbody').on('click', '#remove', function () {
    var removeId = $(this).attr('data-id')
    layer.confirm('确定删除该分类?', { icon: 3, title: '删除分类' }, function (index) {
        //do something
        removeCate(removeId)
        layer.close(index);
    });
})



//封装获取当前id文章分类数据
function getThisBook(id) {
    $.ajax({
        type: 'GET',
        url: '/my/article/cates/' + id,
        success: function (res) {
            if (res.status !== 0) return layer.msg(res.message)
            form.val('bookinfo', res.data)
        }
    })
}

//封装函数实现对当前类别的删除
function removeCate(id) {
    $.ajax({
        type: 'GET',
        url: '/my/article/deletecate/' + id,
        success: function (res) {
            if (res.status !== 0) return layer.msg(res.message)
            layer.msg(res.message)
            getBookList()
        }
    })

}

//封装函数实现修改当前文章类别的操作
function modifyCate(modifyInfo) {
    $.ajax({
        type: 'POST',
        url: '/my/article/updatecate',
        data: modifyInfo,
        success: function (res) {
            if (res.status !== 0) return layer.msg(res.message)
            layer.msg(res.message)
            getBookList()
        }
    })

}


